package net.sk.china.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 阿里 Druid 数据源配置组件
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/2 14:03
 * +----------------------------------------------------------------------
 */

@Configuration
public class DruidConfig {

    @Bean("dataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() { return new DruidDataSource(); }

//    /**
//     * // 主要实现WEB监控的配置处理
//     * @return {@code ServletRegistrationBea}
//     */
//    @Bean
//    public ServletRegistrationBean druidServlet() {
//        // 进行druid监控的配置处理操作
//        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
////        servletRegistrationBean.addInitParameter("allow",
////                "127.0.0.1,192.168.1.159"); // 白名单
////        servletRegistrationBean.addInitParameter("deny", "192.168.1.200"); // 黑名单
//        // 用户名
//        servletRegistrationBean.addInitParameter("loginUsername", "qrsk");
//        // 密码
//        servletRegistrationBean.addInitParameter("loginPassword", "liaolai)(*&434hsd%3");
//        // 是否可以重置数据源
//        servletRegistrationBean.addInitParameter("resetEnable", "true");
//        return servletRegistrationBean ;
//    }
//    @Bean
//    public FilterRegistrationBean filterRegistrationBean() {
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean() ;
//        filterRegistrationBean.setFilter(new WebStatFilter());
//
//        // 所有请求进行监控处理
//        filterRegistrationBean.addUrlPatterns("/*");
//        //不必监控的请求
//        filterRegistrationBean.addInitParameter("exclusions", "*.html,*.png,*.ico,*.js,*.gif,*.jpg,*.css,/druid/*");
//        return filterRegistrationBean ;
//    }
}
